import Vue from 'vue';
import Vuex from 'vuex';
import lodash from 'lodash';
import { getSystemInfo, getSystemParams } from '@/api/common';
Vue.use(Vuex)
const state = {
	siteData: {},
	systemInfo: {},
	userInputForm: []
}

const actions = {
	init: ({ state, dispatch }) => {
		return new Promise(async (resolve, reject) => {
			await dispatch('getSiteData');
			await dispatch('getSystemInfo');
			await dispatch('getSystemParams');
			resolve();
		}).catch(() => {
			reject();
		})
	},

	async getSiteData({ commit }) {
		let datas = {
			title: '课程知识问答（模板）',
			chat_color_theme: null,
			chat_color_theme_inverted: false,
			icon_type: 'emoji',
			icon: '🤖',
			icon_background: '#FFEAD5',
			icon_url: null,
			description: null,
			copyright: null,
			privacy_policy: null,
			custom_disclaimer: '',
			default_language: 'en-US',
			prompt_public: false,
			show_workflow_steps: true,
			use_icon_as_answer_icon: false
		}
		commit('SET_SITE_DATA', datas);
	},

	//获取系统信息
	async getSystemInfo({ commit }) {
		let result = await getSystemInfo();
		if (result.status === 200) {
			document.title = result.data.name;
			commit('SET_SYSTEM_INFO', result.data);
		}
	},

	//获取系统参数
	async getSystemParams({ commit }) {
		let result = await getSystemParams();
		if (result.status === 200) {
			let datas = result.data;
			commit('SET_USER_INPUT_FORM', datas.user_input_form);
		}
	}
}

const mutations = {
	SET_SITE_DATA: (state, d) => {
		state.siteData = d;
		Vue.nextTick(() => {
			let root = document.documentElement;
			root.style.setProperty('--icon-background', d.icon_background);
		})
	},
	SET_SYSTEM_INFO: (state, d) => {
		state.systemInfo = d;
	},
	SET_USER_INPUT_FORM: (state, d) => {
		state.userInputForm = d;
	}
}

export default { state, actions, mutations }